package settings.logic;




import java.util.HashMap;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.wifi.WifiManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;



import com.google.android.maps.GeoPoint;

public class LocationService extends Service implements LocationListener {
	private WifiManager wifiManager;
	private LocationManager location;
	private HashMap<String,Point> puntos;
	
	
	@Override
	public void onCreate() {
		Toast.makeText(getApplicationContext(), "oncreate Service",
				 Toast.LENGTH_SHORT).show();
		puntos= new HashMap<String, Point>();
		
		wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
		//Recogemos el servicio de localizacion
		location=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
		//Registramos el listner para que refresque la ubicacion mediante la red
		location.requestLocationUpdates(location.NETWORK_PROVIDER, 60000, 0, this);
		
		super.onCreate();
	}
	
	public GeoPoint getLocalizacion() {
		// TODO Auto-generated method stub
		
		return null;
	}
	@Override
	public void onLocationChanged(android.location.Location location) {
		// TODO Auto-generated method stub
		
		
		Point p = new Point(true, true,true);
		Double latitudeE6 = (location.getLatitude() * 1E6);
		Double longitudeE6 =  (location.getLongitude() * 1E6);
		GeoPoint a = new GeoPoint(latitudeE6.intValue(), longitudeE6.intValue());
		p.setLugar(a);
		puntos.put("myHouse",p);
		Point b= puntos.get("myHouse");
		if(b.getWifi()){
			wifiManager.setWifiEnabled(true);
		}else{
			wifiManager.setWifiEnabled(false);
		}
		
	}
	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}
	
	public class LocalBinder extends Binder {
        public LocationService getService() {
            return LocationService.this;
        }
    }
	public IBinder onBind(Intent intent) {
        return mBinder;
    }

    // This is the object that receives interactions from clients.  See
    // RemoteService for a more complete example.
    private final IBinder mBinder = new LocalBinder();
    
    public void setPuntos(HashMap<String, Point> puntos) {
		this.puntos = puntos;
	}

}
